<template>
  <el-card style="margin-bottom:20px;">
    <div slot="header" class="clearfix">
      <span>基本信息</span>
    </div>

    <div class="user-profile">
      <div class="box-center" @click="imagecropperShow=true">
        <pan-thumb :image="user.avatar || 'https://wpimg.wallstcn.com/69a1c46c-eb1c-4b46-8bd4-e9e686ef5251.png'" :height="'100px'" :width="'100px'" :hoverable="false">
          <div>你好</div>
          {{ user.account }}
        </pan-thumb>
      </div>
      <div class="box-center">
        <div class="user-name text-center">{{ user.realName || '超级管理员' }}</div>
        <div class="user-role text-center text-muted">{{ user.posName || '超级管理员' }}</div>
      </div>
    </div>

    <div class="user-bio">
      <div class="user-education user-bio-section">
        <div class="user-bio-section-header"><i class="el-icon-location-information" /><span>地址</span></div>
        <div class="user-bio-section-body">
          <div class="text-muted">
            {{ user.address }}
          </div>
        </div>
      </div>

      <div class="user-bio">
        <div class="user-education user-bio-section">
          <div class="user-bio-section-header"><i class="el-icon-location-information" /><span>电子签名</span></div>
          <div class="user-bio-section-body">
            <div class="text-muted">
              <el-image :src="user.signature || 'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg'" lazy />
              <el-button icon="el-icon-edit" type="primary" size="mini" @click="openSignDialog"> 电子签名 </el-button>
            </div>
          </div>
        </div>

        <div class="user-skills user-bio-section">
          <div class="user-bio-section-header"><svg-icon icon-class="skill" /><span>Skills</span></div>
          <div class="user-bio-section-body">
            <div class="progress-item">
              <span>Vue</span>
              <el-progress :percentage="70" />
            </div>
            <div class="progress-item">
              <span>JavaScript</span>
              <el-progress :percentage="18" />
            </div>
            <div class="progress-item">
              <span>Css</span>
              <el-progress :percentage="12" />
            </div>
            <div class="progress-item">
              <span>ESLint</span>
              <el-progress :percentage="100" status="success" />
            </div>
          </div>
        </div>
      </div>
      <image-cropper
        v-show="imagecropperShow"
        :key="imagecropperKey"
        :width="300"
        :height="300"
        url="/api/sysFile/uploadAvatar"
        lang-type="zh"
        @close="close"
        @crop-upload-success="cropSuccess"
      />
      <el-dialog title="电子签名" :visible.sync="signDialogVisible">
        <div style="border: 1px dashed gray; width: 100%; height: 250px">
          <VueSignaturePad ref="signaturePadRef" :options="signOptions" style="background-color: #fff" />
        </div>
        <div style="margin-top: 10px">
          <div style="display: inline">画笔粗细：<el-input-number v-model="signOptions.minWidth" :precision="1" :step="0.1" :max="2.5" @change="changeWidth" /></div>
          <div style="display: inline; margin-left: 30px">画笔颜色：<el-color-picker v-model="signOptions.penColor" style="position: relative;bottom:-9px" size="mini" @change="changeColor" /></div>
        </div>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="unDoSign">撤销</el-button>
            <el-button @click="clearSign">清屏</el-button>
            <el-button type="primary" @click="saveUploadSign">保存</el-button>
          </span>
        </template>
      </el-dialog>
    </div></el-card>
</template>

<script>
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'
import { base64ToFile } from '@/utils/base64Conver'
import { uploadSignature } from '@/api/user'
import Vue from 'vue'
import VueSignaturePad from 'vue-signature-pad'
Vue.use(VueSignaturePad)

export default {
  components: { ImageCropper, PanThumb },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          avatar: '',
          role: ''
        }
      }
    }
  },
  data() {
    return {
      imagecropperShow: false,
      imagecropperKey: 0,
      signDialogVisible: false,
      penColor: '#000000',
      minWidth: 1.0,
      signOptions: {
        penColor: '#000000',
        minWidth: 1.0
      }
    }
  },
  methods: {
    cropSuccess(resData) {
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      this.user.avatar = `${process.env.VUE_APP_BASE_API}${resData.filePath}/${resData.name}`
    },
    close() {
      this.imagecropperShow = false
    },
    openSignDialog() {
      this.signDialogVisible = true
    },
    changeWidth(currentValue) {
      this.minWidth = currentValue
      this.signOptions = {
        penColor: this.penColor,
        minWidth: currentValue
      }
    },
    changeColor(color) {
      this.penColor = color
      this.signOptions = {
        penColor: color,
        minWidth: this.minWidth
      }
    },
    unDoSign() {
      this.$refs.signaturePadRef.undoSignature()
    },
    clearSign() {
      this.$refs.signaturePadRef.clearSignature()
    },
    async saveUploadSign() {
      const { isEmpty, data } = this.$refs.signaturePadRef.saveSignature()
      if (isEmpty) return
      const fmData = new FormData()
      fmData.append('file', base64ToFile(data, this.user.account + '.png'))
      var res = await uploadSignature(fmData)
      this.user.signature = `${process.env.VUE_APP_BASE_API}${res.result.filePath}/${res.result.name}`

      this.clearSign()
      this.signDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
